package com.shilei;

import com.shilei.common.Dir;
import com.shilei.common.Group;
import com.shilei.common.PropertyMgr;
import com.shilei.cor.Collider;
import com.shilei.cor.ColliderChain;

import java.awt.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @description: 门面Facade
 * @author: 81271
 * @date: 2020-04-15 19:45
 */
public class GameModel {
    private final static GameModel INSTANCE = new GameModel();

    static {
        INSTANCE.init();
    }

    private GameModel() {
    }

    private Tank myTank;

    public List<GameObject> gameObjectList = new ArrayList<>();

    public Collider collider = new ColliderChain();

    public void paint(Graphics g) {
//        Color color = g.getColor();
//        g.setColor(Color.WHITE);
//        g.drawString("子弹的数量:" + bulletList.size(), 10, 60);
//        g.drawString("敌人的数量:" + tankList.size(), 10, 80);
//        g.setColor(color);
//
//        tank.paint(g);


        for (int i = 0 ; i < gameObjectList.size() ; i++) {
            gameObjectList.get(i).paint(g);
        }

        for (int i = 0 ; i < gameObjectList.size() ; i++) {
            for (int j = i + 1 ; j < gameObjectList.size() ; j++) {
                collider.collide(gameObjectList.get(i), gameObjectList.get(j));
            }
        }

    }

    private void init() {
        int initTankCount = Integer.parseInt((String) PropertyMgr.get("initTankCount"));

        for (int i = 0 ; i < initTankCount; i++) {
            new Tank(50  + i * 100, 200 , Dir.DOWN, Group.Bad);
        }

        myTank = new Tank(200, 600 , Dir.DOWN, Group.Good);

        new Wall(100, 500);
        new Wall(100, 600);
        new Wall(100, 700);
        new Wall(100, 800);
        new Wall(400, 500);
        new Wall(400, 600);
        new Wall(400, 700);
        new Wall(400, 800);

    }


    public Tank getMainTank() {
        return myTank;
    }

    public static GameModel getInstance() {
        return INSTANCE;
    }

    public void save() {
        File file = new File("e:/java_project/tank.data");
        ObjectOutputStream oos = null;

        try {
            oos = new ObjectOutputStream(new FileOutputStream(file));
            oos.writeObject(myTank);
            oos.writeObject(gameObjectList);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    public void reload() {
        File file = new File("e:/java_project/tank.data");
        ObjectInputStream ois = null;

        try {
            ois = new ObjectInputStream(new FileInputStream(file));
            myTank = (Tank) ois.readObject();
            gameObjectList = (List) ois.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
